﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using NhanVien_BLL;
using NhanVien_Public;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        SqlConnection conn = new SqlConnection("Data source=(local);Initial Catalog=DemoLoadImage;Integrated Security=true");
        nv_public nv = new nv_public();
        nv nhanvienbll = new nv();
        private void Form1_Load(object sender, EventArgs e)
        {
            gridControl1.DataSource = nhanvienbll.LayDuLieu();
            gridControl1.ShowOnlyPredefinedDetails = true;
            
        }



        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog op = new OpenFileDialog();
            op.Filter = "JPG fie(*.jpg|*.jpg|All file(*.*|*.*))";
            op.FilterIndex = 1;
            op.RestoreDirectory = true;
            if (op.ShowDialog() == DialogResult.OK)
            {
                textEdit2.Text = op.FileName;
                pictureBox1.ImageLocation = op.FileName;
            }
        }
       
        private void button1_Click(object sender, EventArgs e)
        {
            hinhanh = conver(textEdit2.Text);
            nv.TenNhanVien = textEdit1.Text;
            nv.Anh = hinhanh;
            nhanvienbll.Insert(nv,nv);
            Form1_Load( sender,e);
        }
        private byte[] hinhanh;
        class1 cls = new class1();
        private void gridControl1_Click(object sender, EventArgs e)
        {
            try
            {
                string i;
                i = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Ten").ToString();
                DataTable dt = new DataTable();
                string sql = "select anh from nhanvien where Ten='" + i + "'";
                dt = nhanvienbll.taobang(sql);
                byte[] stream = (byte[])dt.Rows[0][0];
                Image image;
                using (MemoryStream ms = new MemoryStream(stream, 0, stream.Length))
                {
                    ms.Write(stream, 0, stream.Length);
                    image = Image.FromStream(ms, true);
                }
                pictureBox1.Image = image;

               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }
        private byte[] conver(string url)
        {
            byte[] picbyte =null;
            FileInfo info = new FileInfo(url);
            long numByte = info.Length;
            FileStream fs;
            fs = new FileStream(url,FileMode.Open,FileAccess.Read);
           
            //fs.Read(picbyte, 0, System.Convert.ToInt32(fs.Length));
            //fs.Close();
            BinaryReader br = new BinaryReader(fs);
            picbyte = br.ReadBytes((int)numByte);
            return picbyte;

        }
    }
}
